package com.yunc.modules.sys.service.impl;

import cn.hutool.core.date.DateUtil;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunc.common.properties.Pro;
import com.yunc.modules.sys.entity.SysSendSms;
import com.yunc.modules.sys.mapper.SysSendSmsMapper;
import com.yunc.modules.sys.service.ISysSendSmsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 短信通知记录表 service实现
 *
 * @author concong tao
 * @since 2019-02-17
 */
@Service
public class SysSendSmsServiceImpl extends ServiceImpl<SysSendSmsMapper, SysSendSms> implements ISysSendSmsService {

    @Autowired
    private SysSendSmsMapper sysSendSmsMapper;
    @Autowired
    private Pro pro;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean snedAndSave(SysSendSms sysSendSms) {
        DefaultProfile profile = DefaultProfile.getProfile("default", pro.getAliSendSmsAccessKeyId(), pro.getAliSendSmsAccessSecret());
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        request.putQueryParameter("PhoneNumbers", sysSendSms.getPhone());
        request.putQueryParameter("SignName", "浙江追月电气有限公司");
        if (StringUtils.equals(sysSendSms.getStatus(), "1")) {
            request.putQueryParameter("TemplateCode", "SMS_157683895");
            // TemplateParam格式为
            request.putQueryParameter("TemplateParam", "{\"product\":\"" + sysSendSms.getProduct() + "\"" +
                    ", \"logistics\":\"" + sysSendSms.getLogistics() + "\",\"code\":\"" + sysSendSms.getCode() + "\",\"address\":\"" + sysSendSms.getAddress() + "\"}");
        } else {
            request.putQueryParameter("TemplateCode", "SMS_157683893");
            // TemplateParam格式为
            request.putQueryParameter("TemplateParam", "{\"product\":\"" + sysSendSms.getProduct() + "\"" +
                    ", \"logistics\":\"" + sysSendSms.getLogistics() + "\",\"code\":\"" + sysSendSms.getCode() + "\",\"company\":\"" + sysSendSms.getAddress() + "\"}");
        }
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getHttpStatus());
            if (response.getHttpStatus() == 200) {
                sysSendSms.setCreateTime(DateUtil.date());
                return this.save(sysSendSms);
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }
}
